package cn.shopping.controller.members;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shopping.pojo.Au_user;
import cn.shopping.pojo.hat_area;
import cn.shopping.pojo.hat_caty;
import cn.shopping.pojo.hat_province;
import cn.shopping.pojo.hat_total;
import cn.shopping.service.members.membersService;

@Controller
@RequestMapping("/member")
public class membersController {

	@Resource
	private membersService membersservice;
	/**
	 * 得到省份
	 */
	@RequestMapping("/province")
	@ResponseBody
	public List<hat_province> getprovince() {
		List<hat_province> list = membersservice.getprovince();
		return list;
	}
	/**
	 * 城市
	 * @return
	 */
	@RequestMapping("/city")
	@ResponseBody
	public List<hat_caty> getcity(@RequestParam("provinceNo")String num) {
		List<hat_caty> list = membersservice.getcity(num);
		return list;
	}
	
	/**
	 * 县、区
	 * @return
	 */
	@RequestMapping("/area")
	@ResponseBody
	public List<hat_area> getarea(@RequestParam("cityNo")String num) {
		List<hat_area> list = membersservice.getarea(num);
		return list;
	}
	
	/**
	 * 添加会员
	 */
	
	@RequestMapping("/addmember.html")
	public String addmember(
			@RequestParam("user.user")String username,
			@RequestParam("user.xm")String phone,
			@RequestParam("user.password")String password,
			@RequestParam("user.secondpassword")String secondpassword,
			@RequestParam("user.sfz")String tuijianren,
			@RequestParam("user.phone")String phoneoftjr,
			@RequestParam("user.idcard")String idcard,
			@RequestParam("user.yhkh")String bank,
			@RequestParam("user.yhkhId")String bankid,
			@RequestParam(value="user.zhzt",defaultValue="0")Integer status,
			@RequestParam("shengfen")String province,
			@RequestParam("chengshi")String city,
			@RequestParam("quyu")String area,
			HttpSession session,
			Model model) {
		Au_user usersa = new Au_user();
		hat_total total = new hat_total();
		total.setHat_province(province);
		total.setHat_city(city);
		total.setHat_area(area);
		if(membersservice.addtotle(total)){
			usersa.setAddress(membersservice.seltotlemax());
			usersa.setBankId(bankid);
			usersa.setBankDeposit(bank);
			usersa.setCardTypeName(idcard);
			usersa.setReferrer(tuijianren);
			usersa.setReferrerPhone(phoneoftjr);
			usersa.setSecondaryPassword(secondpassword);
			usersa.setUserName(username);
			usersa.setUserPassword(password);
			usersa.setUserPhone(phone);
			usersa.setMemberState(status);
			//引用方法或接口之前需要看方法是否需要什么参数，以及什么类型的参数
			usersa.setStartday(new Date(System.currentTimeMillis()));
			if(membersservice.addmember(usersa)){
				return "redirect:/member/userlist.html";
			}
			membersservice.deltotle(membersservice.seltotlemax());
			return "huiyuanzhuce";
		}

		return "huiyuanzhuce";
	}
	
	
	/**
	 * 会员列表
	 */
	
	@RequestMapping("/userlist.html")
	public String userlist(Model model,
			@RequestParam(value="Idcode",defaultValue="",required = false) String idcode,
			@RequestParam(value="memberNames",defaultValue="",required = false) String userName
			) {
		List<Au_user> list = membersservice.getalluser(idcode,userName);
		model.addAttribute("userlist", list);
		return "yonghuliebiao";
	}
	
	
	/**
	 * 删除会员
	 */
	
	@RequestMapping("/deluser.html")
	public String deluser(@RequestParam("Userid")Integer userid,Model model) {
		if (!membersservice.delofuserbyid(userid)) {
			model.addAttribute("error", "删除失败！");
		}
		return "redirect:/member/userlist.html";
	}
	
	/**
	 * 修改会员信息
	 */
	
	@RequestMapping("/selByid.html")
	public String selByid(@RequestParam("Userid")Integer userid,HttpSession session,Model model) {
		
		Au_user users = membersservice.getserbyid(userid);
		model.addAttribute("byidUser", users);
		session.setAttribute("totles", users.getTotal());
		return "huiyuanxiugai";
	}
	
	@RequestMapping("/modifyuser.html")
	public String modifyuser(
			@RequestParam("user.id")Integer userid,
			@RequestParam("user.xm")String phone,
			@RequestParam("user.password")String password,
			@RequestParam("user.secondpassword")String secondpassword,
			@RequestParam("user.phone")String phoneoftjr,
			@RequestParam("user.yhkh")String bank,
			@RequestParam("user.yhkhId")String bankid,
			@RequestParam(value="user.zhzt",defaultValue="0")Integer status,
			@RequestParam(value="shengfen",defaultValue="0")String province,
			@RequestParam(value="chengshi",defaultValue="0")String city,
			@RequestParam(value="quyu",defaultValue="0")String area,
			HttpSession session,
			Model model) {
		
		hat_total totalold = (hat_total)session.getAttribute("totles");
		Au_user usersa = membersservice.getserbyid(userid);
		hat_total total = new hat_total();
		int num = totalold.getHat_id();
		if (!province.equals("0") && !city.equals("0") && !area.equals("0")) {
			total.setHat_id(num);
			total.setHat_province(province);
			total.setHat_city(city);
			total.setHat_area(area);
		}else{
			total = totalold;
		}
		if (membersservice.modifytotal(total)) {
			usersa.setBankId(bankid);
			usersa.setBankDeposit(bank);
			usersa.setReferrerPhone(phoneoftjr);
			usersa.setSecondaryPassword(secondpassword);
			usersa.setUserPassword(password);
			usersa.setUserPhone(phone);
			usersa.setMemberState(status);
			//引用方法或接口之前需要看方法是否需要什么参数，以及什么类型的参数
			usersa.setModifyday(new Date(System.currentTimeMillis()));
			if (membersservice.mmodifyuserbtid(usersa)) {
				return "redirect:/member/userlist.html";
			}
			membersservice.modifytotal(totalold);
			return "redirect:/member/selByid.html?Userid="+userid;
		}
		
		return "redirect:/member/selByid.html?Userid="+userid;
			}
	
	
	
	
	
	
	
	
}
